/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.common.dto.Code;
import com.javaeye.common.service.ICodeService;

public class ListUtil {
	
	protected static Log log = LogFactory.getLog(ListUtil.class);
	
	/**
	 * 单位
	 */
	public static final String UNIT_CODE_CATEGORY = "001";
	
	/**
	 * 部门
	 */
	public static final String UNIT_CODE_DEPATMENT = "002";
	
	/**
	 * 供应商属性组ID，注意固定的属性组都是以0000开头
	 * 以后加的一般属性，应该从0010开始编号
	 */
	public static final String FIELD_GROUP_SUPPLIER = "000001";
	
	/**
	 * 客户属性组ID，注意固定的属性组都是以0000开头
	 * 以后加的一般属性，应该从0010开始编号
	 */
	public static final String FIELD_GROUP_CUSTOMER = "000002";
	
	/**
	 * 颜色属性组ID，注意固定的属性组都是以0000开头
	 * 以后加的一般属性，应该从0010开始编号
	 */
	public static final String FIELD_GROUP_COLOR = "000003";
	
	/**
	 * 采购订单编号前缀
	 */
	public static final String SUPPLIER_ORDER_ID_PRE = "A";
	
	/**
	 * 销售订单编号前缀
	 */
	public static final String CUSTOMER_ORDER_ID_PRE = "B";
	
	/**
	 * 领退料单编号前缀
	 */
	public static final String MATERIALS_REQUISITE_ORDER_ID_PRE = "C";
	
	/**
	 * 成品入库单编号前缀
	 */
	public static final String PRODUCT_INSTORE_ORDER_ID_PRE = "D";
	
	/**
	 * 库存盘点单编号前缀
	 */
	public static final String MATERIALS_INVENTORY_CHECK_ID_PRE = "E";
	
	
	public static final Integer[] MATERIALS_CATEGORY_PROCUREMENT = new Integer[]{
		26,//原料
		27,//辅料
		59//包装材料
	};
	
	private static List<ListItem> codeCategoryItems;
	
	/**
	 * 订单状态
	 */
	private static List<ListItem> orderItems;
	
	/**
	 * 物流方式
	 */
	private static List<ListItem> logisticsTypeItems;
	
	/**
	 * 用户职务
	 */
	private static List<ListItem> userDutyItems;
	
	private static List<ListItem> checkResultItems;
	
	static {
		
		codeCategoryItems = new ArrayList<ListItem>();
		codeCategoryItems.add(new ListItem(UNIT_CODE_CATEGORY, "单位"));
		codeCategoryItems.add(new ListItem(UNIT_CODE_DEPATMENT, "部门"));
		
		checkResultItems = new ArrayList<ListItem>();
		checkResultItems.add(new ListItem("1", "合格"));
		checkResultItems.add(new ListItem("2", "不合格"));
		checkResultItems.add(new ListItem("3", "宽放"));
		checkResultItems.add(new ListItem("4", "特采放行"));
		
		orderItems = new ArrayList<ListItem>();
		orderItems.add(new ListItem("1", "等待计划安排"));
		orderItems.add(new ListItem("2", "等待业务确认"));
		orderItems.add(new ListItem("3", "等待理货"));
		orderItems.add(new ListItem("4", "等待检验"));
		orderItems.add(new ListItem("5", "等待复检"));
		orderItems.add(new ListItem("6", "等待发货"));
		orderItems.add(new ListItem("7", "等待关闭"));
		
		logisticsTypeItems = new ArrayList<ListItem>();
		logisticsTypeItems.add(new ListItem("1", "公司合约货运"));
		logisticsTypeItems.add(new ListItem("2", "公司自用车辆"));
		logisticsTypeItems.add(new ListItem("3", "公司临时货运"));
		logisticsTypeItems.add(new ListItem("4", "客户自运"));
		logisticsTypeItems.add(new ListItem("5", "业务员提货"));
		logisticsTypeItems.add(new ListItem("6", "出口货柜"));
		logisticsTypeItems.add(new ListItem("7", "其他"));
		
		userDutyItems = new ArrayList<ListItem>();
		userDutyItems.add(new ListItem("1", "职员"));
		userDutyItems.add(new ListItem("2", "经理"));
		userDutyItems.add(new ListItem("3", "总经理"));
		
	}
	
	/**
	 * 检查是否是采购原料的分类
	 * @return
	 */
	public static boolean checkIsProcurementCategory(int categoryCode) {
		for(int code : MATERIALS_CATEGORY_PROCUREMENT) {
			if (categoryCode == code) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 取得常用数据分类列表
	 * @return
	 */
	public static List<ListItem> getCodeCategoryList() {
		return codeCategoryItems;
	}
	
	/**
	 * 取得所有的单位列表
	 * @return
	 */
	public static List<ListItem> baseCodeList(ICodeService service, String categoryId) {
		//PCS ROLLS KGS m2
		List<ListItem> units = new ArrayList<ListItem>();
		List<Code> list = service.getCodesByCategoryId(categoryId);
		for (Code c : list) {
			units.add(new ListItem(String.valueOf(c.getId()), c.getName()));
		}
		return units;
	}
	
	/**
	 * 取得Id对应的单位名称
	 * @return
	 */
	public static String baseCodeName(ICodeService service, String codeId) {
		return service.getCodeName(codeId);
	}
	
	/**
	 * 取得所有的检验结果列表
	 * @return
	 */
	public static List<ListItem> checkResultList() {
		return checkResultItems;
	}
	
	/**
	 * 取得Id对应的检验结果名称
	 * @return
	 */
	public static String checkResultName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(checkResultItems, index);
	}
	
	/**
	 * 取得所有的用户职务列表
	 * @return
	 */
	public static List<ListItem> userDutyList() {
		return userDutyItems;
	}
	
	/**
	 * 取得Id对应的用户职务名称
	 * @return
	 */
	public static String userDutyName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(userDutyItems, index);
	}
	
	/**
	 * 取得所有的订单状态
	 * @return
	 */
	public static List<ListItem> orderStatusList() {
		return orderItems;
	}
	
	/**
	 * 取得Id对应的订单状态
	 * @return
	 */
	public static String orderStatusName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(orderItems, index);
	}
	
	/**
	 * 取得所有的物流方式
	 * @return
	 */
	public static List<ListItem> logisticsTypeList() {
		return logisticsTypeItems;
	}
	
	/**
	 * 取得Id对应的物流方式
	 * @return
	 */
	public static String logisticsTypeName(String id) {
		//log.debug("取得Id(" + id + ")对应的物流方式");
		int index = Integer.parseInt(id) - 1;
		return getItemName(logisticsTypeItems, index);
	}
	
	private static String getItemName(List<ListItem> list, int index) {
		if (index >= list.size() || index < 0) {
			return "";
		}
		return list.get(index).getValue();
	}
	
}
